<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-if="!isHistory" @click="myHandleAdd" type="primary" v-has="'hasDailyYksEquipmentDevopsListPermission:add'" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('运维主表')">导出</a-button>
      <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <!-- <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown> -->
    </div>

    <!-- table区域-begin -->
    <div>
      <!-- <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div> -->
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <span style="float:right;">
          <a @click="loadData()"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <list-columns-fix-setter
            v-if="columns.length > 0"
            v-model="columns"
            :def-columns="columns"
            :listName="listName"
            @change="columnChange"
          />
        </span>
      </div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <div v-if="!isHistory">
            <a @click="myHandleDetail(record)">详情</a>
            <a-divider v-if="record.actionsEnabled.edit" type="vertical" />
            <a v-if="record.actionsEnabled.edit" @click="myHandleEdit(record)">编辑</a>
            <a-divider v-if="record.actionsEnabled.deleted" type="vertical" />
            <a-popconfirm v-if="record.actionsEnabled.deleted" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
            <a-divider  v-if="record.actionsEnabled.approve"  type="vertical" />
            <a v-if="record.actionsEnabled.approve" @click="() => myApproveShow(record.id)"> 审批</a>
            <a-divider v-if="record.actionsEnabled.confirm"  type="vertical" />
            <a-popconfirm v-if="record.actionsEnabled.confirm" title="确认内容吗?" @confirm="() => myConfirmShow(record.id)">
              <a>客户确认</a>
            </a-popconfirm>
            <a-divider v-if="record.actionsEnabled.cancel" type="vertical" />
            <a-popconfirm v-if="record.actionsEnabled.cancel" title="确认取消吗?" @confirm="() => myCancelShow(record.id)">
              <a>取消</a>
            </a-popconfirm>
          </div>
          <div v-else>
            <a-divider v-if="record.actionsEnabled.run"  type="vertical" />
            <a-popconfirm v-if="record.actionsEnabled.run" title="确认执行吗?" @confirm="() => myRunShow(record)">
              <a>计划执行</a>
            </a-popconfirm>
          </div>
          <!-- <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                
              </a-menu-item>
              <a-menu-item >
              </a-menu-item>
              <a-menu-item >

              </a-menu-item>
              <a-menu-item >

              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
        </span>

      </a-table>
    </div>

    <yks-equipment-devops-service-modal ref="modalForm" @ok="modalFormOk"/>
    <confirm-modal ref="confirmModal"       
      @myHandleConfirm="myHandleConfirm"
      @myHandleUnConfirm="myHandleUnConfirm" />
  </a-card>
</template>

<script>

  import { httpAction, getAction ,putAction} from '@/api/manage'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import YksEquipmentDevopsServiceModal from './modules/YksEquipmentDevopsServiceModal'
  import '@/assets/less/TableExpand.less'
  import ConfirmModal from './modules/ConfirmModal.vue'
    import ListColumnsFixSetter from '@/components/ListColumnsFixSetter'

  export default {
    name: "YksEquipmentDevopsServiceList",
    mixins:[JeecgListMixin],
    components: {
      YksEquipmentDevopsServiceModal,
      ConfirmModal,
      ListColumnsFixSetter,
    },
    data () {
      return {
        description: '运维主表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'运维计划状态',
            align:"center",
            sorter: true,
            dataIndex: 'status_dictText',
            filters: [
            {
              text: '草稿',
              value: '1',
            },
            {
              text: '待审核',
              value: '2',
            },
                        {
              text: '待确认',
              value: '3',
            },
            {
              text: '待执行',
              value: '4',
            },
                        {
              text: '取消',
              value: '5',
            },
            {
              text: '完成',
              value: '6',
            },],
          },
          {
            title:'运维类型',
            align:"center",
            dataIndex: 'devopsPersontype_dictText'
          },
          {
            title:'触发类型',
            align:"center",
            dataIndex: 'triggerType_dictText'
          },
          {
            title:'初次触发时间',
            align:"center",
            dataIndex: 'triggerTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'重复次数',
            align:"center",
            dataIndex: 'repeatNumber'
          },
          {
            title:'重复间隔',
            align:"center",
            dataIndex: 'repeatGap'
          },
          {
            title:'重复间隔单位',
            align:"center",
            dataIndex: 'repeatGapUnit_dictText'
          },
          {
            title:'上次保养时间',
            align:"center",
            dataIndex: 'maintenanceLastTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'下次机会保养时间',
            align:"center",
            dataIndex: 'maintenanceNextTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'保养时间段开始',
            align:"center",
            dataIndex: 'maintenanceQuantumstart',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'保养时间段结束',
            align:"center",
            dataIndex: 'maintenanceQuantumend',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'保养内容',
            align:"center",
            dataIndex: 'maintenanceContext'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          itemStatus: '/srv/yksEquipmentDevopsService/status/confirm',
          list: "/srv/yksEquipmentDevopsService/list",
          delete: "/srv/yksEquipmentDevopsService/delete",
          deleteBatch: "/srv/yksEquipmentDevopsService/deleteBatch",
          exportXlsUrl: "/srv/yksEquipmentDevopsService/exportXls",
          importExcelUrl: "srv/yksEquipmentDevopsService/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
        currentEquipmentId:'',
        serviceType:'',
        isHistory:false,
        serviceTableChange:'',
        listName:'YksEquipmentDevopsServiceList',
        actionsEnabled:{
          approve: false,
          cancel: false,
          complete: false,
          deleted: false,
          edit: false,
          suspend: false,
        },
      }
    },
    created() {
      // this.isHistory = this.setIsHistory()
      this.getSuperFieldList();
    },
    watch:{
      serviceTableChange : {
        deep:true,
        handler(val,oval) {
          console.log(val,'.....')
          if (val == '3') {
            this.isHistory = true
            this.loadData()
          } else {
            this.isHistory = false
          }
          console.log(this.isHistory)
        }
      },

    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
    setIsHistory() {      
        // if(this.$route.path.indexOf('EquipmentDevopsCombineList') != -1&&this.serviceType!='daily'){
        //   return true
        // }
        // return false
        // this.loadData()
    },
    columnChange(columns) {
         this.columns = columns
      },
    //审批
    myHandleConfirm(content,recordId) {
      console.log(content)
      // this.$refs[this.activeKey].myHandleConfirm(content)
      this.myHandleItem(recordId,"3",content?content:'审批通过','审批')
    },
    //拒绝
    myHandleUnConfirm(content,recordId) {
      console.log(content)
      // this.$refs[this.activeKey].myHandleUnConfirm(content)
      this.myHandleItem(recordId,"1",content?content:'审批不通过','拒绝')
    },
    myConfirmShow(recordId) {
      this.myHandleItem(recordId,"4","客户确认",'客户确认')
    },
    myRunShow(record) {
      // this.myHandleItem(recordId,"6","计划执行",'计划执行')
      console.log('项目id:'+record.equipmentId)        
      this.$refs.modalForm.currentEquipmentId = record.equipmentId
      this.$refs.modalForm.serviceType = this.serviceType
      this.$refs.modalForm.isHistory = true
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.disableSubmit = false
    },
    myCancelShow(recordId) {
      this.myHandleItem(recordId,"5","运维计划取消",'取消')
    },
    //显示审批界面
    myApproveShow(recordId) {
      this.$refs.confirmModal.recordId = recordId
      this.$refs.confirmModal.show()
    },
    myHandleAdd: function () {        
      this.$refs.modalForm.currentEquipmentId = this.currentEquipmentId
      this.$refs.modalForm.serviceType = this.serviceType
      this.$refs.modalForm.isHistory = false
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    myHandleDetail(record) {
      this.$refs.modalForm.isHistory = false
      this.handleDetail(record)
    },
    myHandleEdit(record) {
      console.log('项目id:'+record.equipmentId)        
      this.$refs.modalForm.currentEquipmentId = record.equipmentId
      this.$refs.modalForm.serviceType = this.serviceType
      this.$refs.modalForm.isHistory = false
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.disableSubmit = false
    },
    myHandleItem(itemId,status,remark,msg){
        let that = this
        putAction(this.url.itemStatus+"?ids="+itemId+"&status="+status + "&remark=" + remark,{}).then(res=>{
          if(res.success){
            this.$message.success(msg+"成功！")
            that.onClearSelected()
            that.loadData()
          }else{
            this.$message.error(res.message)
          }
        })
      },
      open(record,serviceType) {
        this.currentEquipmentId = record.id
        this.serviceType=serviceType
        this.loadData(1)
      },
      loadData(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        if(this.$route.path.indexOf('OpmProcessBomList') != -1){
          table.$el.querySelector(".ant-table-pagination").style.top = "-70px"
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();//查询条件
        if (this.isHistory) {
          params.status = "4"
        }
        params.equipmentId = this.currentEquipmentId
        // params.devopsPersontype=this.serviceType
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          }else{
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'name',text:'名称',dictCode:''})
        fieldList.push({type:'string',value:'devopsPersontype',text:'运维类型',dictCode:''})
        fieldList.push({type:'string',value:'triggerType',text:'触发类型',dictCode:''})
        fieldList.push({type:'date',value:'triggerTime',text:'初次触发时间'})
        fieldList.push({type:'string',value:'repeatNumber',text:'重复次数',dictCode:''})
        fieldList.push({type:'string',value:'repeatGap',text:'重复间隔',dictCode:''})
        fieldList.push({type:'string',value:'repeatGapUnit',text:'重复间隔单位',dictCode:''})
        fieldList.push({type:'date',value:'maintenanceLastTime',text:'上次保养时间'})
        fieldList.push({type:'date',value:'maintenanceNextTime',text:'下次机会保养时间'})
        fieldList.push({type:'date',value:'maintenanceQuantumstart',text:'保养时间段开始'})
        fieldList.push({type:'date',value:'maintenanceQuantumend',text:'保养时间段结束'})
        fieldList.push({type:'string',value:'maintenanceContext',text:'保养内容',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>